package com.android.mios.util;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
/*
 * 
 */
public class BillListView extends ExpandableListView implements OnScrollListener {


	private ViewGroup mViewGroup;
	public int iGroupIndex = -1;
	private ExpandableListAdapter mExpandableListAdapter;

	/**
	 * @param context
	 */
	public BillListView(Context context) {
		super(context);
		super.setOnScrollListener(this);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public BillListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		super.setOnScrollListener(this);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public BillListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		super.setOnScrollListener(this);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		if (mExpandableListAdapter == null)
			mExpandableListAdapter = this.getExpandableListAdapter();

		int iPosition = view.pointToPosition(0, 0);
		if (iPosition != AdapterView.INVALID_POSITION) {
			BillListView mBillListView = (BillListView)view;
			long lPos = mBillListView.getExpandableListPosition(iPosition);
			int iGroupPosition = ExpandableListView.getPackedPositionGroup(lPos);
			int iChildPosition = ExpandableListView.getPackedPositionChild(lPos);

			if (iChildPosition < 0) 
				iGroupPosition = -1;
			
			if (iGroupPosition < iGroupIndex) {
				iGroupIndex = iGroupPosition;
				if (mViewGroup != null){
					mViewGroup.removeAllViews();
					mViewGroup.setVisibility(GONE);//��ʧ�����Ҳ�ռ�κοռ�
				}
			} else if (iGroupPosition > iGroupIndex) {
				final FrameLayout mFrameLayout = (FrameLayout) this.getParent();//��BillListView֮��һ��ҪǶ��FrameLayout
				iGroupIndex = iGroupPosition;
				if (mViewGroup != null)
					mFrameLayout.removeView(mViewGroup);
				mViewGroup = (ViewGroup) getExpandableListAdapter()
						.getGroupView(iGroupPosition, false, null, null);
				mViewGroup.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						collapseGroup(iGroupIndex);
						mFrameLayout.removeView(mViewGroup);
						BillListView.this.setSelectedGroup(iGroupIndex);
					}
				});
				mFrameLayout.addView(mViewGroup,mFrameLayout.getChildCount(), new LayoutParams(
						LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

			}
		}
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}
}